package com.shanyu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.shanyu.dao.ICatalogDao;
import com.shanyu.dao.IItemDao;
import com.shanyu.dao.IItemPropDao;
import com.shanyu.dao.IItemSkuDao;
import com.shanyu.dao.ISkuDao;
import com.shanyu.dto.CatalogDto;
import com.shanyu.dto.ItemDto;
import com.shanyu.eo.CatalogEo;
import com.shanyu.eo.ItemEo;
import com.shanyu.eo.ItemPropEo;
import com.shanyu.eo.ItemSkuEo;
import com.shanyu.eo.SkuEo;
import com.shanyu.service.ICatalogService;

@Service
public class CatalogServiceImpl implements ICatalogService{

	@Autowired
	private ICatalogDao catalogDao;
	
	@Autowired
	private IItemDao itemDao;
	
	@Autowired
	private ISkuDao skuDao;
	
	@Autowired
	private IItemPropDao itemPropDao;
	
	@Autowired
	private IItemSkuDao itemSkuDao;

	public List<CatalogEo> getAllCatalogList() {
		// TODO Auto-generated method stub
		return catalogDao.select(new CatalogEo());
	}

	public List<CatalogDto> getAllCatalogDtoList() {
		// TODO Auto-generated method stub
		List<CatalogDto> res = new ArrayList<CatalogDto>();
		CatalogEo query = new CatalogEo();
		List<CatalogEo> list = catalogDao.select(query);
		if(!CollectionUtils.isEmpty(list)){
			for(int i = 0; i < list.size(); i++){
				CatalogDto catalogDto = new CatalogDto();
				BeanUtils.copyProperties(list.get(i), catalogDto);
				ItemEo itemQuery = new ItemEo();
				itemQuery.setCatalogId(list.get(i).getId());
				List<ItemEo> itemList = itemDao.select(itemQuery);
				List<ItemDto> itemDtoList = new ArrayList<ItemDto>();
				if(!CollectionUtils.isEmpty(itemList)){
					for(int j = 0; j < itemList.size(); j++){
						ItemDto itemDto = new ItemDto();
						BeanUtils.copyProperties(itemList.get(j), itemDto);
						//查询sku关联
						ItemSkuEo itemSkuQuery = new ItemSkuEo();
						itemSkuQuery.setItemId(itemList.get(j).getId());
						List<ItemSkuEo> itemSkuList = itemSkuDao.select(itemSkuQuery);
						List<String> skuIds = new ArrayList();
						for(int k = 0; k < itemSkuList.size(); k++){
							skuIds.add(itemSkuList.get(k).getSkuId());
						}
						if(!CollectionUtils.isEmpty(skuIds)){
							List<SkuEo> skuList = skuDao.selectBySkuIds(skuIds);
							itemDto.setSkuList(skuList);
						}
						
						
						ItemPropEo propQuery = new ItemPropEo();
						propQuery.setItemId(itemList.get(j).getId());
						List<ItemPropEo> propList = itemPropDao.select(propQuery);
						itemDto.setPropList(propList);
						
						itemDtoList.add(itemDto);
					}
				}
				if(1 == catalogDto.getCode()){
					catalogDto.setProp(true);
				}else{
					catalogDto.setProp(false);
				}
				
				catalogDto.setDeadLine(catalogDto.getCode());
				catalogDto.setItemList(itemDtoList);
				res.add(catalogDto);
			}
		}
		return res;
	}
	
	
	
	
	
}
